package com.infore.airsupervise.ui.fragment;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.beardedhen.androidbootstrap.BootstrapButton;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.infore.airsupervise.R;
import com.infore.airsupervise.adapter.FileListAdapter;
import com.infore.airsupervise.ui.widget.ItemDecoration1px;
import com.rey.material.app.BottomSheetDialog;
import com.vincent.filepicker.Constant;
import com.vincent.filepicker.activity.NormalFilePickActivity;
import com.vincent.filepicker.filter.entity.NormalFile;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.finalteam.galleryfinal.FunctionConfig;
import cn.finalteam.galleryfinal.GalleryFinal;
import cn.finalteam.galleryfinal.model.PhotoInfo;

import static android.app.Activity.RESULT_OK;
import static com.vincent.filepicker.activity.NormalFilePickActivity.SUFFIX;

/**
 * 发布任务 文件 图片选择fragment
 * Created by xiaotao on 2017/6/9.
 */

public class MissionFileSelectFragment extends BaseFragment implements View.OnClickListener{

    @BindView(R.id.rv_file)
    RecyclerView rvFile;

    private final String [] file_suffix = new String[]{"xls","txt","xlsx","doc","docx","pdf","ppt","pptx"};
    private final int REQUEST_CODE_GALLERY = 1001;

    private List<String> file_list = new ArrayList<>();
    private FileListAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mission_file_select, container, false);
        unbinder = ButterKnife.bind(this, view);

        initRecyclerView(getContext());

        return view;
    }

    /**
     * 初始化文件recyclerView
     */
    private void initRecyclerView(Context context){
        rvFile.setLayoutManager(new LinearLayoutManager(context));
        rvFile.addItemDecoration(new ItemDecoration1px(context,ItemDecoration1px.VERTICAL_LIST));
        adapter = new FileListAdapter(R.layout.item_file, file_list,true);
        rvFile.setAdapter(adapter);
    }

    /**
     * 底部文件选择弹框
     */
    private void initBottomDialog(Context context){
        BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(context);
        View contextView = LayoutInflater.from(context).inflate(R.layout.layout_mission_file_bottom,null);
        //选择文件
        BootstrapButton btn_file = (BootstrapButton) contextView.findViewById(R.id.btn_file);
        //选择图片
        BootstrapButton btn_picture = (BootstrapButton) contextView.findViewById(R.id.btn_picture);
        btn_file.setOnClickListener(this);
        btn_picture.setOnClickListener(this);

        bottomSheetDialog.contentView(contextView)
                .heightParam(ViewGroup.LayoutParams.WRAP_CONTENT)
                .show();
    }

    /**
     * 打开相册
     */
    private void selectInGallery() {
        //带配置
        FunctionConfig config = new FunctionConfig.Builder()
                .setMutiSelectMaxSize(5)
                .setEnablePreview(true)
                .setEnableCamera(true)
                .build();
        GalleryFinal.openGalleryMuti(REQUEST_CODE_GALLERY, config, mOnHanlderResultCallback);
    }
    /**
     * GalleryFinal 相册 的 回调方法
     */
    private GalleryFinal.OnHanlderResultCallback mOnHanlderResultCallback = new GalleryFinal.OnHanlderResultCallback() {
        @Override
        public void onHanlderSuccess(int reqeustCode, List<PhotoInfo> resultList) {
            if (null != resultList) {
                for (PhotoInfo photo : resultList) {
                    file_list.add(photo.getPhotoPath());
                }
                // filesList.addAll(ImageUtil.getInstance().compressOnUpload(resultList)); 压缩图片
                adapter.notifyDataSetChanged();
            }
        }

        @Override
        public void onHanlderFailure(int requestCode, String errorMsg) {

        }
    };

    /**
     * 文件选择
     */
    @OnClick(R.id.btn_select)
    public void onViewClicked() {
        initBottomDialog(getContext());

    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_file :
                Intent intent  = new Intent(getActivity(), NormalFilePickActivity.class);
                intent.putExtra(Constant.MAX_NUMBER,5);
                intent.putExtra(SUFFIX,file_suffix);
                startActivityForResult(intent, Constant.REQUEST_CODE_PICK_FILE);
                break;
            case R.id.btn_picture :
                selectInGallery();
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK ){
            if(requestCode == Constant.REQUEST_CODE_PICK_FILE){
                ArrayList<NormalFile> fileSelectedList= data.getParcelableArrayListExtra(Constant.RESULT_PICK_FILE);
                for(NormalFile e : fileSelectedList){
                    file_list.add(e.getPath());
                }
                adapter.notifyDataSetChanged();
            }
        }
    }

    public List<String> getFileList(){
        return file_list;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if(mOnHanlderResultCallback != null){
            mOnHanlderResultCallback = null ;
        }
    }
}
